package com.yswy.theater.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.common.utils.redis.RedisCache;
import com.yswy.common.utils.redis.RedisConstants;
import com.yswy.common.vo.dto.BaseFilter;
import com.yswy.theater.entity.TheaterPlay;
import com.yswy.theater.entity.TheaterPlayGather;
import com.yswy.theater.entity.TheaterPlayLookLog;
import com.yswy.theater.mapper.TheaterPlayGatherMapper;
import com.yswy.theater.service.TheaterPlayGatherService;
import com.yswy.theater.service.TheaterPlayLookLogService;
import com.yswy.theater.vo.req.ListTheaterPlayGatherReqVo;
import com.yswy.theater.vo.res.ListTheaterPlayGatherResVo;
import com.yswy.theater.vo.res.TheaterPlayGatherResVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class TheaterPlayGatherServiceImpl extends ServiceImpl<TheaterPlayGatherMapper, TheaterPlayGather>
        implements TheaterPlayGatherService {
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private TheaterPlayServiceImpl playService;
    @Autowired
    private TheaterPlayLookLogService theaterPlayLookLogService;
    @Override
    public ListTheaterPlayGatherResVo ListTheaterPlayGather(ListTheaterPlayGatherReqVo listTheaterPlayGatherReqVo, Long userId , Boolean isVip) {
        Page<TheaterPlayGatherResVo> page = baseMapper.ListTheaterPlayGather(listTheaterPlayGatherReqVo.getPlayId() ,userId
                , new Page<BaseFilter>(listTheaterPlayGatherReqVo.getPageIndex(), listTheaterPlayGatherReqVo.getPageSize()));
        List<TheaterPlayGatherResVo> list = page.getRecords();
        TheaterPlay theaterPlay =playService.getById(listTheaterPlayGatherReqVo.getPlayId());
        TheaterPlayLookLog theaterPlayLookLog =theaterPlayLookLogService.getOne(new LambdaQueryWrapper<TheaterPlayLookLog>()
                .eq(TheaterPlayLookLog::getTheaterPlayId , listTheaterPlayGatherReqVo.getPlayId())
                .eq(TheaterPlayLookLog::getUserId , userId)
                .last("limit 1"));
        for (int i = 0; i < list.size(); i++) {
            TheaterPlayGatherResVo t = list.get(i);
            if (isVip){
                t.setIsUnlock(1);
            }else {
                if (i <theaterPlay.getDefUnlockNum()){
                    t.setIsUnlock(1);
                }else {
                    if (t.getIsUnlock() == null){
                        t.setIsUnlock(0);
                    }
                }
            }
        }
        page.setRecords(list);
        Integer episodes = 1 ;
        Boolean ifCollect = false ;
        if (theaterPlayLookLog != null){
            episodes = theaterPlayLookLog.getEpisodes() ;
            if (theaterPlayLookLog.getIfCollect() == 1){
                ifCollect = true ;
            }

        }
        return new ListTheaterPlayGatherResVo(page , theaterPlay , ifCollect ,episodes );
    }
    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return RedisConstants.SYS_CONFIG_KEY + configKey;
    }
}
